package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public String login(User user) {
        //1、获取明文
        String password = user.getPassword();

        //2、将明文加密为密文
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());
        //3、在user里更新密文
        user.setPassword(md5);
        //4、查询密码是否正确
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        //5、将查询的对象封装成对象，进行判断
        User userDB = userMapper.selectOne(queryWrapper);
        if(userDB==null){
            return null;
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        return token;
    }

    @Override
    public void addUser(User user) {

        String password = user.getPassword();
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5)
            .setStatus(true);
            /*.setCreated(new Date())
            .setUpdated(user.getCreated());//创建时间和修改时间要保持一致*/
        userMapper.insert(user);
    }
    /*mybatis的方式 并在mapper层加了@select注解来分页查询*/
    /*@Override
    public PageResult getUserlist(PageResult pageResult) {
        long total = userMapper.selectCount(null);
        int size = pageResult.getPageSize();
        int start = (pageResult.getPageNum()-1)*size;
        List<User> userList = userMapper.findListByPage(start,size);
        pageResult.setTotal(total).setRows(userList);

        return pageResult;
    }*/

    /*利用mp方式查询数据库*/
    @Override
    public PageResult getUserlist(PageResult pageResult){
        //1、定义分页对象
        IPage<User> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        //2、定义条件构造器，指定动态查询sql
        boolean flag = StringUtils.hasLength(pageResult.getQuery());//判断是否为空的工具api
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag, "username", pageResult.getQuery());
        //3、进行分页查询
        page = userMapper.selectPage(page,queryWrapper);
        //4、从封装后的分页对象中获取数据
        pageResult.setTotal(page.getTotal())
                  .setRows(page.getRecords());
        return pageResult;
    }

    @Override
    public void status(User user) {
        userMapper.updateById(user);
    }

    @Override
    //事务的注解
    @Transactional//(noRollbackFor = {RuntimeException.class})
    public void deleteUser(Integer id) {
        //int i = 1/0;设置算数异常后前端不能删除
        userMapper.deleteById(id);
    }



    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }


}
